import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 懒加载组件（按模块分组）
const Nav = () => import('../views/home/nav.vue')
const Home = () => import('../views/home/home.vue')
const Category = () => import('../views/home/category.vue')
const Cart = () => import('../views/home/cart.vue')
const User = () => import('../views/home/user.vue')

const Goodlist = () => import('@/views/goods/goodlist.vue')
const Goodsdesc = () => import('@/views/goods/goodsdesc.vue')

const Login = () => import('@/views/login/login.vue')
const Formlogin = () => import('@/views/login/formlogin.vue')
const Smslogin = () => import('@/views/login/smslogin.vue')
const Register = () => import('@/views/login/register.vue')

const Useredit = () => import('@/views/users/useredit.vue')
const Orderlist = () => import('@/views/order/orderlist.vue')
const Orderconfirm = () => import('@/views/order/orderconfirm.vue')
const Pay = () => import('@/views/order/pay.vue')

const Addresslist = () => import('@/views/address/addresslist.vue') // 修正文件夹名 ress -> address
const Addressadd = () => import('@/views/address/addressadd.vue')
const Editress = () => import('@/views/address/editress.vue') // 建议文件名改为 editAddress.vue 保持规范

const SearchPage = () => import('@/views/search/SearchPage.vue')

const routes = [
  // 底部导航相关页面（共享Nav组件）
  {
    path: '/',
    name: 'nav',
    component: Nav,
    children: [
      {
        path: '/',
        name: 'home',
        component: Home,
        meta: { title: '首页' }
      },
      {
        path: '/category',
        name: 'category',
        component: Category,
        meta: { title: '分类' }
      },
      {
        path: '/cart',
        name: 'cart',
        component: Cart,
        meta: {
          title: '购物车',
          requiresAuth: true // 需要登录
        }
      },
      {
        path: '/user',
        name: 'user',
        component: User,
        meta: {
          title: '我的',
          requiresAuth: true // 需要登录
        }
      },
      {
        path: '/goodslist',
        name: 'goodslist',
        component: Goodlist,
        meta: { title: '商品列表' }
      },
      {
        path: '/goodsdesc',
        name: 'goodsdesc',
        component: Goodsdesc,
        meta: { title: '商品详情' }
      },

      // 搜索相关
      {
        path: '/search',
        name: 'search',
        component: SearchPage,
        meta: { title: '搜索' }
      },

      // 登录注册相关
      {
        path: '/login',
        name: 'login',
        component: Login,
        meta: { title: '登录' }
      },
      {
        path: '/formlogin',
        name: 'formlogin',
        component: Formlogin,
        meta: { title: '账号密码登录' }
      },
      {
        path: '/smslogin',
        name: 'smslogin',
        component: Smslogin,
        meta: { title: '手机号登录' }
      },
      {
        path: '/register',
        name: 'register',
        component: Register,
        meta: { title: '注册' }
      },

      // 用户相关（需登录）
      {
        path: '/useredit',
        name: 'useredit',
        component: Useredit,
        meta: {
          title: '编辑资料',
          requiresAuth: true
        }
      },

      // 订单相关（需登录）
      {
        path: '/orderlist',
        name: 'orderlist',
        component: Orderlist,
        meta: {
          title: '订单列表',
          requiresAuth: true
        }
      },
      {
        path: '/orderconfirm',
        name: 'orderconfirm',
        component: Orderconfirm,
        meta: {
          title: '确认订单',
          requiresAuth: true
        }
      },
      {
        path: '/pay',
        name: 'pay',
        component: Pay,
        meta: {
          title: '支付',
          requiresAuth: true
        }
      },

      // 地址相关（需登录）
      {
        path: '/addresslist',
        name: 'addresslist',
        component: Addresslist,
        meta: {
          title: '地址列表',
          requiresAuth: true
        }
      },
      {
        path: '/addressadd',
        name: 'addressadd',
        component: Addressadd,
        meta: {
          title: '添加地址',
          requiresAuth: true
        }
      },
      {
        path: '/editress',
        name: 'editress',
        component: Editress,
        meta: {
          title: '编辑地址',
          requiresAuth: true
        }
      }
    ]
  },

  // 商品相关页面
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

// 全局导航守卫：统一处理登录权限
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || '严选商城'

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (token) {
      next() // 已登录，放行
    } else {
      next({ name: 'login' }) // 未登录，跳转到登录页
    }
  } else {
    next() // 不需要登录，直接放行
  }
})

export default router